package com.it.service.impl;

import com.it.domain.Account;
import com.it.mapper.AccountMapper;
import com.it.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("accountService")
@Transactional(propagation = Propagation.REQUIRED,readOnly = false)//事务
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)//事务
    public List<Account> findAll() {
        List<Account> accounts = accountMapper.findAll();
//        for (Account account : accounts) {
//            System.out.println(account);
//        }
        return accounts;
    }

    @Override
    public void transfer(String sourceName, String targetName, Float money){
        System.out.println("开始转账");
        //查询转出账户
        Account source = accountMapper.findByName(sourceName);
        //查询转入账户
        Account target = accountMapper.findByName(targetName);
        //转出账户捡钱
        source.setMoney(source.getMoney()-money);
        //转入账户加钱
        target.setMoney(target.getMoney()+money);
        //更新转出账户
        accountMapper.update(source);
        //模拟转账异常
        int i=1/0;
        //更新转入账户
        accountMapper.update(target);
    }
}
